package com.smg.community.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.smg.community.model.City;

@Service
public class CityManager {
	
	private static List<City> CityList  = new ArrayList<City>();
	
	static {
		City city = new City();
		city.setCityname("上海");
		city.setCityno(21);
		CityList.add(city);
		city = new City();
		city.setCityname("北京");
		city.setCityno(10);
		CityList.add(city);
		city = new City();
		city.setCityname("广州");
		city.setCityno(20);
		CityList.add(city);
		
		
		
	}
	public static List<City> getAllCity(){
		return CityList;
	}
	/**
	 * @param userCityNos
	 * @return 将用户拥有的城市权限,21,10, 转换成 cityList
	 */
	public static  List<City> getCityListByUserCityNos(String userCityNos) {
		List<City> userCityList = new ArrayList<City>();
		if ( userCityNos.length() == 0){
			return userCityList ;
		}
		userCityNos = userCityNos.substring(1);
		userCityNos = userCityNos.substring(0,userCityNos.length()-1);
		String[] cityNoArray =userCityNos.split(",");
		for ( String cityNo:cityNoArray){
			City city = getCityByCityNo(Integer.parseInt(cityNo));
			if ( city != null ){
				userCityList.add(city);
			}
		}
		return userCityList;
	}
	
	public static  City getCityByCityNo(int cityNo){
		for(City city:CityList){
			if ( city.getCityno() == cityNo){
				return city;
			}
		}
		return null;
	}
}
